package com.tianji.learning.mq;

import com.tianji.common.constants.MqConstants;
import com.tianji.learning.domain.dto.SignInMessage;
import com.tianji.learning.enums.PointsRecordType;
import com.tianji.learning.service.IPointsRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 积分消费者
 *
 * @Author wangxinyi
 * @Date 2025/1/11 17:29
 */
@Component
@Slf4j
public class ChangeScoreListener {

    @Autowired
    private IPointsRecordService pointsRecordService;

    /**
     * 签到
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = "sign.points.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.SIGN_IN))
    public void listenSignInMessage(SignInMessage message) {
        log.info("签到消费者收到消息了，收到的消息为{}", message);
        if (message.getPoints() == null || message.getUserId() == null) {
            log.error("签到消费者收到消息了，消息体数据有误{}", message);
        }
        //新增学习积分记录
        pointsRecordService.addSignScore(message.getUserId(), message.getPoints(), PointsRecordType.SIGN);
    }


    // 监听新增互动问答事件
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "qa.points.queue", durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.LEARNING_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.WRITE_REPLY
    ))
    public void listenWriteReplyMessage(Long userId) {
        log.info("互动问答消费者收到消息了，收到的消息为{}", userId);
        pointsRecordService.addSignScore(userId, 5, PointsRecordType.QA);
    }

}
